package com.chen.controller;

import com.chen.pojo.Person;
import com.chen.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class EmployeeController {
    @Autowired
    private PersonService personService;

    @RequestMapping("/emps")
    public String tables(Model model) {
        List<Person> personList = personService.list();
        model.addAttribute("emps", personList);
        return "emp/table";
    }

    @GetMapping("/emp")
    public String toAddPage() {
        return "emp/add";
    }

    @PostMapping("/emp")
    public String addEmp(Person person) {
        System.out.println(person);
        personService.insert(person);
        return "redirect:/emps";
    }

    @GetMapping("/emp/{id}")
    public String updateE(@PathVariable("id") Integer id, Model model) {
        Person byId = personService.getById(id);
        model.addAttribute("emp", byId);
        List<Person> personList = personService.list();
        model.addAttribute("emps", personList);
        return "emp/update";
    }
    @PostMapping("/updateEmp")
    public String update(Person person)
    {
        personService.update(person);
        return "redirect:/emps";
    }
    @GetMapping("/delemp/{id}")
    public String deleteEmp(@PathVariable("id")Integer id)
    {
        personService.delete(id);
        return "redirect:/emps";
    }

}
